<?php
class AppController extends Controller {
    public $components = array(
        'Acl',
        'Auth' => array(
            'authorize' => 'actions',
            'loginRedirect' => array(
                'admin' => false,
                'controller' => 'users',
                'action' => 'dashboard'
            )
        ),
        'Session'
    );
    
    function beforeFilter() {
        $user = $this->Auth->user();
        if (!empty($user)) {
          Configure::write('User', $user[$this->Auth->getModel()->alias]);
        }        
    }
  
    public function beforeRender() {
        $user = $this->Auth->user();
        Controller::loadModel('Group');
        
        if (!empty($user)) {
          $user = $user[$this->Auth->getModel()->alias];
          $group = $this->Group->find('first', array('conditions' => array('Group.id'=>$this->Auth->user('group_id'))));
        }
        //Update TyGia    
        $this->set(compact('user'));
        $this->set(compact('group'));
        
    }
    
    public function snippet($text,$length=64,$tail="...") {
		$text = trim(strip_tags($text));
		$txtl = strlen($text);
		if($txtl > $length) {
		    for($i=1;$text[$length-$i]!=" ";$i++) {
			if($i == $length) {
			    return substr($text,0,$length) . $tail;
			}
		    }
		    $text = substr($text,0,$length-$i+1) . $tail;
		}
		return $text;
    }
    
    public function getImage($text) {
        preg_match( '/img(.*?)src\=\"(.*?)\"/', $text, $match );
	//echo $match[0];
	//var_dump($match);
        return isset($match[2]) ? $match[2] : 0;
  }
}
?>